package lib.bone.audit.workload.service.impl;

import cn.hutool.core.lang.Validator;
import lib.bone.audit.workload.dao.WorkloadUseLogDAO;
import lib.bone.audit.workload.domain.WorkloadUseLog;
import lib.bone.audit.workload.service.WorkloadUseLogService;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class WorkloadUseLogServiceImpl implements WorkloadUseLogService {

    @Autowired
    WorkloadUseLogDAO workloadUseLogDAO;

    @Override
    public List<WorkloadUseLog> findByExample(Example<WorkloadUseLog> example) {
        return workloadUseLogDAO.findAll(example);
    }

    @Override
    @Transactional
    public void deleteAll(List<WorkloadUseLog> logs) {
        workloadUseLogDAO.deleteAll(logs);
    }

    @Override
    public List<WorkloadUseLog> findByUserIdAndCycleId(Long userId, String cycleId) {
        return workloadUseLogDAO.findByUserIdAndCycleId(userId,cycleId);
    }

    @Override
    public List<WorkloadUseLog> getUseLog(String workloadId) {
        return workloadUseLogDAO.findByWorkloadId(workloadId);

    }

    @Override
    public void saveList(List<WorkloadUseLog> logs) {
        workloadUseLogDAO.saveAll(logs);
    }

    @Override
    public PageResultDTO<WorkloadUseLog> getAll(Pageable pageable, Example<WorkloadUseLog> example) {
        Page<WorkloadUseLog> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = workloadUseLogDAO.findAll(pageable);
        }else {
            page = workloadUseLogDAO.findAll(example,pageable);
        }
        List<WorkloadUseLog> list = page.getContent();
        PageResultDTO<WorkloadUseLog> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return pageResultDTO;
    }

    @Override
    public ResponseDTO deleteById(String id) {
        //判断数据是否存在
        Optional<WorkloadUseLog> workloadUseLogOp = workloadUseLogDAO.findById(id);
        if(!workloadUseLogOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+id+"]对应数据不存在");
        workloadUseLogDAO.deleteById(id);
        return ResponseDTO.succMsg("删除成功!");
    }

    @Override
    public Optional<WorkloadUseLog> getById(String id) {
        return workloadUseLogDAO.findById(id);
    }

    @Override
    public WorkloadUseLog saveOne(WorkloadUseLog workloadUseLog) {
        if (Validator.isNotEmpty(workloadUseLog.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return workloadUseLogDAO.save(workloadUseLog);
    }

    @Override
    public List<WorkloadUseLog> findByUserId(Long userId) {
        return workloadUseLogDAO.findByUserId(userId);
    }

    @Override
    public WorkloadUseLog updateOne(WorkloadUseLog workloadUseLog) {
        if (Validator.isEmpty(workloadUseLog.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return workloadUseLogDAO.save(workloadUseLog);
    }

//    @Override
//    public ResponseDTO<WorkloadUseLog> saveOne(WorkloadUseLogAddDTO workloadUseLogAddDTO) {
//        WorkloadUseLog workloadUseLog = new WorkloadUseLog();
//        BeanUtil.copyProperties(workloadUseLogAddDTO,workloadUseLog, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
//        return ResponseDTO.succData(workloadUseLogDAO.save(workloadUseLog));
//    }
//
//    @Override
//    public ResponseDTO<WorkloadUseLog> updateOne(WorkloadUseLogUpdateDTO workloadUseLogUpdateDTO) {
//        Optional<WorkloadUseLog> workloadUseLogOp = getById(workloadUseLogUpdateDTO.getId());
//        if(!workloadUseLogOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+
//                workloadUseLogUpdateDTO.getId()+"]对应的科研冲抵不存在");
//        WorkloadUseLog workloadUseLog = workloadUseLogOp.get();
//        BeanUtil.copyProperties(workloadUseLogUpdateDTO,workloadUseLog,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
//        return ResponseDTO.succData(workloadUseLogDAO.save(workloadUseLog));
//    }

}
